package com.hejia.bulbul.controller;

import com.hejia.alauda.entity.PayGateBank;
import com.hejia.alauda.entity.SaleActivity;
import com.hejia.alauda.entity.SaleActivityDevice;
import com.hejia.alauda.service.SaleActivityService;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * Created by jimi on 14/12/3.
 */
@Controller
@RequestMapping("saleActivity")
public class SalesActivityController extends BaseController {

	@Resource
	private SaleActivityService saleActivityService;

	@RequestMapping()
	public String saleActivities(Pager<SaleActivity> pager, Model model) {
		pager = saleActivityService.pagerSaleActivity(pager);

		PagerHelper<SaleActivity> helper = new PagerHelper<>();
		helper.setPager(pager);
		helper.setBaseUrl("/saleActivity");

		model.addAttribute("pagerHelper", helper);
		return "/product/saleactivity/saleactivitys";
	}

	/**
	 * 活动添加页面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "/product/saleactivity/add";
	}

	/**
	 * 新增或者修改
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String addAction(SaleActivity saleActivity,
			@RequestParam(value = "saleactivityIds") Long[] saleactivityIds,
			RedirectAttributes redirectAttributes) {
		if (saleActivity != null) {
			// 精确到天计算时差
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				long endTime = format.parse(
						format.format(saleActivity.getEndTime())).getTime();
				long now = format.parse(format.format(new Date())).getTime();
				if (now > endTime) {
					saleActivity.setIsFinish(true);
				} else {
					saleActivity.setIsFinish(false);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

			if (saleActivity.getId() != null) {
				saleActivityService.updateEntity(saleActivity);
				for (Long id : saleactivityIds) {
					if (id == null) {
						continue;
					}
					SaleActivityDevice saleActivityDevice = this.saleActivityService
							.findSaleActivityDeviceById(id);
					saleActivityDevice.setSaleActivity(saleActivity);
					saleActivityDevice.setModifyDate(new Date());
					saleActivityService
							.modifySaleActivityDevice(saleActivityDevice);
				}
				redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
				redirectAttributes.addFlashAttribute("msg", "活动修改成功");
			} else {
				if (saleActivityService.insertEntity(saleActivity)) {
					for (Long id : saleactivityIds) {
						if (id == null) {
							continue;
						}
						SaleActivityDevice saleActivityDevice = this.saleActivityService
								.findSaleActivityDeviceById(id);
						saleActivityDevice.setSaleActivity(saleActivity);
						saleActivityDevice.setModifyDate(new Date());
						saleActivityService
								.modifySaleActivityDevice(saleActivityDevice);
					}
					redirectAttributes.addFlashAttribute("status",
							STATUS_SUCCESS);
					redirectAttributes.addFlashAttribute("msg", "活动添加成功");
				}
			}
		}
		return "redirect:/saleActivity";
	}

	/**
	 * 点击修改跳转到修改的页面
	 */
	@RequestMapping(value = "update/{saleActivityId}", method = RequestMethod.GET)
	public String updateSaleActivity(@PathVariable Long saleActivityId,
			Model model) {
		SaleActivity saleActivity = saleActivityService
				.findSaleActivityById(saleActivityId);
		List<SaleActivityDevice> salist = saleActivityService
				.getSaleActivityDeviceById(saleActivityId);
		model.addAttribute("saleActivity", saleActivity);
		model.addAttribute("salist", salist);
		return "/product/saleactivity/add";
	}

	/**
	 * 上架
	 */
	@RequestMapping(value = "up/{saleActivityId}", method = RequestMethod.GET)
	public String up(@PathVariable Long saleActivityId,
			RedirectAttributes redirectAttributes) {
		if (saleActivityService.upSaleActivity(saleActivityId)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "上线成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "上线失败");
		}
		return "redirect:/saleActivity";
	}

	/**
	 * 下架
	 */
	@RequestMapping(value = "down/{saleActivityId}", method = RequestMethod.GET)
	public String down(@PathVariable Long saleActivityId,
			RedirectAttributes redirectAttributes) {
		if (saleActivityService.downSaleActivity(saleActivityId)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "下线成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "下线失败");
		}
		return "redirect:/saleActivity";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{saleActivityId}", method = RequestMethod.GET)
	public String delete(@PathVariable Long saleActivityId,
			RedirectAttributes redirectAttributes) {
		if (saleActivityService.updateSaleActivityDisable(saleActivityId)) {
			saleActivityService.deleteSaleActivityDeviceBySAId(saleActivityId);
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "删除失败");
		}
		return "redirect:/saleActivity";
	}

}
